package org.ajax.servlet;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.ajax.dao.GoodsDao;
import org.ajax.dao.impl.GoodsDaoImpl;
import org.ajax.entity.Good;

import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author CSY
 * @date 2024-11-20
 */
@WebServlet("/getGood.do")
public class GetGoodServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json;charset=UTF-8");
        String goodId = req.getParameter("goodId");

        GoodsDao dao = new GoodsDaoImpl();
        Good good = dao.selectGoodById(goodId);

        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();

        String json = gson.toJson(good);

        PrintWriter out = resp.getWriter();

        out.println(json);

        out.flush();
        out.close();
    }
}
